<template>
    <div class="article-card">
      <img :src="article.image" :alt="article.title" class="card-image" />
      <div class="card-content">
        <div class="card-category" :class="getCategoryClass(article.categoryId)">
          {{ getCategoryName(article.categoryId) }}
        </div>
        <h3 class="card-title">{{ article.title }}</h3>
        <p class="card-excerpt">{{ article.excerpt }}</p>
        <div class="card-footer">
          <span class="card-date">
            <i class="fas fa-calendar-alt"></i> {{ formatDate(article.date) }}
          </span>
          <button class="read-more-btn" @click="handleReadMore">
            阅读更多 <i class="fas fa-arrow-right"></i>
          </button>
        </div>
      </div>
    </div>
  </template>
  
  <script lang="ts" setup>
  import { computed, defineProps, defineEmits } from 'vue'
  
  // 定义文章类型
  interface Article {
    id: number
    title: string
    excerpt: string
    content: string
    date: string
    categoryId: number
    image: string
    author: string
    views: number
  }
  
  // 定义分类数据
  const categories = [
    { id: 1, name: "饮食健康", color: "bg-green-100 text-green-800" },
    { id: 2, name: "疾病预防", color: "bg-blue-100 text-blue-800" },
    { id: 3, name: "儿童健康", color: "bg-purple-100 text-purple-800" },
    { id: 4, name: "心理健康", color: "bg-pink-100 text-pink-800" },
    { id: 5, name: "老年健康", color: "bg-amber-100 text-amber-800" },
    { id: 6, name: "孕期保健", color: "bg-teal-100 text-teal-800" }
  ]
  
  // 定义 props
  const props = defineProps<{
    article: Article
  }>()
  
  // 定义 emits
  const emits = defineEmits(['read-more'])
  
  // 获取分类名称
  const getCategoryName = (categoryId: number) => {
    const category = categories.find(c => c.id === categoryId)
    return category ? category.name : '未分类'
  }
  
  // 获取分类样式
  const getCategoryClass = (categoryId: number) => {
    const category = categories.find(c => c.id === categoryId)
    return category ? category.color : 'bg-gray-100 text-gray-800'
  }
  
  // 格式化日期
  const formatDate = (dateString: string) => {
    const date = new Date(dateString)
    return date.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric' })
  }
  
  // 处理阅读更多事件
  const handleReadMore = () => {
    emits('read-more', props.article)
  }
  </script>
  
  <style scoped lang="scss">
  .article-card {
    background: var(--white);
    border-radius: 15px;
    overflow: hidden;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
    transition: var(--transition);
    display: flex;
    flex-direction: column;
    height: 100%;
    
    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    }
  }
  
  .card-image {
    width: 100%;
    height: 200px;
    object-fit: cover; // 修正了object: cover;为object-fit: cover;
    object-position: center;
  }
  
  .card-content {
    padding: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  
  .card-category {
    display: inline-block;
    padding: 5px 12px;
    border-radius: 20px;
    font-size: 0.85rem;
    margin-bottom: 12px;
    font-weight: 500;
  }
  
  .card-title {
    font-family: "Poppins", sans-serif;
    font-size: 1.3rem;
    color: var(--text-dark);
    margin-bottom: 10px;
    line-height: 1.4;
  }
  
  .card-excerpt {
    color: var(--text-medium);
    margin-bottom: 20px;
    line-height: 1.6;
    flex: 1;
  }
  
  .card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-light);
    font-size: 0.9rem;
  }
  
  .read-more-btn {
    background: var(--primary-blue);
    color: white;
    border: none;
    border-radius: 20px;
    padding: 8px 15px;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 0.9rem;
  }
  
  .read-more-btn:hover {
    background: var(--dark-blue);
  }
  </style>
  